<?php
	class PictureAction extends CommonAction{
		
		public function index(){
			$model = M('Archives');
			$map = array(
				'type' 		=> 'picture'
			);
			$count = $model->where($map)->count('id');
	        import("ORG.Util.Page");
			$p = new Page($count, C('LIST_BOARD_NUMBER'));
	        //分页查询数据
	        $list = $model
	        	->where($map)->order('id DESC')
	        	->limit($p->firstRow . ',' . $p->listRows)->select();
			
			$this->assign('list', $list);
	        $this->assign("page", $p->show());
	        Cookie::set('_currentUrl_', __SELF__);
			$this->display();
		}
		
		public function edit(){
			$model = M('Archives');
	        //$id = $_REQUEST[$model->getPk()];
	        $vo = $model->find($_GET['id']);
	        $this->assign('vo', $vo);
	        $this->display();
		}

		public function insert(){
			$model = D ( 'Archives' );
			
			$d = $_POST;
			$d['create_time'] = time();
			$d['author'] = $_SESSION[C('USER_AUTH_KEY')];
			$d['content'] = stripslashes($_POST['content']);
			
			if ( $insert = $model->add( $d ) ){
				D('Category')->where("id='".$_POST['cid']."'")->setInc('count');
				$this->checkGroupPic( $insert );
				$this->saveTag($_POST['tags'],$insert); //处理tags
				$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
				$this->success ('新增成功!');
			}else {
				//失败提示
				$this->error ('新增失败!');
			}
			
		}
		
		public function update(){
			$model = D('Archives');
			
			$d = $_POST;
			$d['update_time']		= time();
			$d['content']			= stripslashes( $d['content'] );
			
			if ( $model->where("id='".$d['id']."'")->save($d) ){
				$this->checkGroupPic();
				$this->saveTag($d['tags'],$d['id']); //处理tags
				$this->success ('修改成功!');
			}else{
				$this->error ('修改失败!');
			}
		}
		
		/**
		 * ===================================================
		 * 删除图集
		 *
		 * ===================================================
		 */
		public function delete(){
			if ( isset( $_GET['id'] ) ){
				$this->eDelete( 'picture' , $_GET['id'] );
			}
		}
		
		private function checkGroupPic( $cid=0 ){
			
			if ( $cid != 0 ){
				$cid = (int)$cid;
			}else{
				$cid = (int)$_POST['id'];
			}
			
			if ( !empty( $_POST['pics_keep'] ) ){
				$p_model = D('Picture');
				foreach ( $_POST['pics_keep'] as $key => $val ){
					$p['cid'] = $cid;
					$p['create_time'] = time();
					$p['folder'] = $_POST['pics_folder'][$key];
					$p['ext'] = $_POST['pics_extension'][$key];
					$p['size'] = $_POST['pics_size'][$key];
					$p['file'] = $_POST['pics_name'][$key];
					$p['name'] = $_POST['pics_text'][$key];
					$p['status'] = 1;
					$p_model->add( $p );
				}
			}
			$this->doafterAction();
		}
		
		public function delpicture(){
			$model = D('Picture');
			
			$list = $model->where("id='".$_GET['item']."'")->find();
			$file_path = './'.C('WEB_PUBLIC_PATH').'/'.C('DIR_UPLOAD_PATH').'/'.$list['folder'].'/';
			
			//删除文件
			@unlink($file_path.'resize_'.$list['file']);
			@unlink($file_path.'thumb_'.$list['file']);
			@unlink($file_path.'mini_'.$list['file']);
			@unlink($file_path.$list['file']);
			
			if ($model->where("id='".$_GET['item']."'")->delete()){
				$this->ajaxReturn(1,1,1);
			}else{
				$this->ajaxReturn(0,0,0); //删除失败
			}
			
		}
		
		
		
		public function _before_add(){
			$this->getCategoryOption('picture');
		}
		
		public function _before_edit(){
			$this->getCategoryOption('picture', $_GET['cid']);
		}
	}
?>